package com.ambulance.project.biz.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ambulance.framework.web.domain.AjaxResult;
import com.ambulance.project.gen.domain.GenTableColumn;
import com.ambulance.project.gen.util.VueCustomerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 *  @description 自定义代码生成接口，发送json报文即可json格式如下

{
"requestName": "myDoctory",
"requestCommon": "医生",
"fields": [
{
"columnName": "name",
"columnComment": "姓名"
},
{
"columnName": "name1",
"columnComment": "姓名1"
}
]
}

 *  @author zhao
 *  @date: 2020/5/11
 *  @version: v1.0
 */

@RestController
@RequestMapping(value = "/my/v1/code", method = RequestMethod.POST)
public class CodeImportController {

    @Autowired
    private VueCustomerUtils vueCustomerUtils;

    /**
     * 发送json报文 格式如下
     *
     * @param token
     * @param body
     * @return
     */
    @RequestMapping(value = "/import")
    public Object codeImport(@RequestHeader(value = "token", required = false) String token,
                             @RequestBody String body) {
        JSONObject jsonObject = JSON.parseObject(body);
        String tableName = jsonObject.getString("requestName");
        String tableCommon = jsonObject.getString("requestCommon");
        JSONArray fields = jsonObject.getJSONArray("fields");

        List<GenTableColumn> genTableColumns = null;
        try {
            genTableColumns = JSONArray.parseArray(fields.toJSONString(), GenTableColumn.class);
        } catch (Exception e) {
            return AjaxResult.error("解析错误");
        }


//        for (int i = 0; i < size; i++) {
//            JSONObject fieldsJSONObject = fields.getJSONObject(i);
//
//            String filed = fieldsJSONObject.getString("columnName");
//            String common = fieldsJSONObject.getString("columnComment");
//
//            //dto.setField(filed);
//            //dto.setComment(common);
//            list.add(dto);
//        }

        vueCustomerUtils.doImport(tableName, tableCommon, genTableColumns);

        return AjaxResult.success();
    }
}
